package com.teacher.game.framework.util;

import android.graphics.Color;

/**
 * 粒子特效预设工厂类
 * 为游戏中的各种事件提供预设的粒子特效
 */
public class ParticleEffects {
    
    /**
     * 创建跳跃特效 - 增强版
     * 包含地面尘土飞扬和角色周围的能量粒子
     */
    public static void createEnhancedJumpEffect(ParticleSystem system, float x, float y) {
        // 地面尘土效果
        createGroundDustEffect(system, x, y + 20);
        
        // 角色周围的能量粒子
        createEnergyBurstEffect(system, x, y - 10, Color.rgb(173, 216, 230), 0.6f);
    }
    
    /**
     * 创建地面尘土效果
     */
    private static void createGroundDustEffect(ParticleSystem system, float x, float y) {
        int particleCount = 6;
        float baseSpeed = 80.0f;
        
        for (int i = 0; i < particleCount; i++) {
            // 水平扩散，向上飞溅
            float velX = (-40 + (float)Math.random() * 80);
            float velY = -20 - (float)Math.random() * 40;
            
            // 土黄色到棕色
            int brown = Color.rgb(139, 69, 19);
            int tan = Color.rgb(210, 180, 140);
            int color = (Math.random() > 0.5) ? brown : tan;
            
            float size = 1 + (float)Math.random() * 2;
            float life = 0.4f + (float)Math.random() * 0.8f;
            
            system.emitAdvancedParticle(x + (-10 + (float)Math.random() * 20), y, 
                                      velX, velY, life, color, size,
                                      Particle.TYPE_RECT, 150.0f, 0.95f);
        }
    }
    
    /**
     * 创建能量爆发效果
     */
    private static void createEnergyBurstEffect(ParticleSystem system, float x, float y, 
                                              int color, float intensity) {
        int particleCount = (int)(8 * intensity);
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float speed = 60 + (float)Math.random() * 40;
            float velX = (float)Math.cos(angle) * speed;
            float velY = (float)Math.sin(angle) * speed - 30; // 向上偏移
            
            float size = 2 + (float)Math.random() * 3;
            float life = 0.3f + (float)Math.random() * 0.4f;
            
            system.emitAdvancedParticle(x, y, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.94f);
        }
    }
    
    /**
     * 创建下蹲滑行特效
     */
    public static void createDuckSlideEffect(ParticleSystem system, float x, float y) {
        // 连续的滑行尾迹
        int particleCount = 4;
        
        for (int i = 0; i < particleCount; i++) {
            float velX = -50 - (float)Math.random() * 30; // 向后飞散
            float velY = -10 + (float)Math.random() * 20;
            
            int color = Color.rgb(192, 192, 192); // 灰色尘土
            float size = 1.5f + (float)Math.random() * 1.5f;
            float life = 0.2f + (float)Math.random() * 0.3f;
            
            system.emitAdvancedParticle(x - 15, y + 15, velX, velY, life, color, size,
                                      Particle.TYPE_POINT, 100.0f, 0.97f);
        }
    }
    
    /**
     * 创建超级碰撞爆炸特效 - 增强版
     */
    public static void createMegaExplosionEffect(ParticleSystem system, float x, float y) {
        // 主爆炸环
        createExplosionRing(system, x, y, 1.0f);
        
        // 次级爆炸环
        createExplosionRing(system, x, y, 0.6f);
        
        // 冲击波粒子
        createShockwaveEffect(system, x, y);
        
        // 火花四溅
        createSparkEffect(system, x, y);
    }
    
    /**
     * 创建爆炸环效果
     */
    private static void createExplosionRing(ParticleSystem system, float x, float y, float intensity) {
        int particleCount = (int)(20 * intensity);
        float baseSpeed = 180 * intensity;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float speed = baseSpeed * (0.8f + (float)Math.random() * 0.4f);
            float velX = (float)Math.cos(angle) * speed;
            float velY = (float)Math.sin(angle) * speed;
            
            // 火焰颜色渐变：红->橙->黄
            int red = 255;
            int green = (int)(Math.random() * 200);
            int blue = (int)(Math.random() * 50);
            int color = Color.rgb(red, green, blue);
            
            float size = 4 * intensity + (float)Math.random() * 3;
            float life = 0.6f * intensity + (float)Math.random() * 0.4f;
            
            system.emitAdvancedParticle(x, y, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 120.0f, 0.91f);
        }
    }
    
    /**
     * 创建冲击波效果
     */
    private static void createShockwaveEffect(ParticleSystem system, float x, float y) {
        int particleCount = 8;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float velX = (float)Math.cos(angle) * 250;
            float velY = (float)Math.sin(angle) * 250;
            
            int color = Color.WHITE;
            float size = 6 + (float)Math.random() * 2;
            float life = 0.3f;
            
            system.emitAdvancedParticle(x, y, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.85f);
        }
    }
    
    /**
     * 创建火花效果
     */
    private static void createSparkEffect(ParticleSystem system, float x, float y) {
        int particleCount = 25;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)(Math.random() * 2 * Math.PI);
            float speed = 100 + (float)Math.random() * 200;
            float velX = (float)Math.cos(angle) * speed;
            float velY = (float)Math.sin(angle) * speed;
            
            int color = (Math.random() > 0.5) ? Color.YELLOW : Color.WHITE;
            float size = 1 + (float)Math.random() * 2;
            float life = 0.8f + (float)Math.random() * 0.7f;
            
            system.emitAdvancedParticle(x, y, velX, velY, life, color, size,
                                      Particle.TYPE_POINT, 200.0f, 0.93f);
        }
    }
    
    /**
     * 创建连击得分特效
     */
    public static void createComboScoreEffect(ParticleSystem system, float x, float y, 
                                            int score, int combo) {
        // 主分数文字 - 根据连击数调整颜色
        int textColor;
        if (combo <= 1) {
            textColor = Color.YELLOW;
        } else if (combo <= 3) {
            textColor = Color.rgb(255, 165, 0); // 橙色
        } else if (combo <= 5) {
            textColor = Color.RED;
        } else {
            textColor = Color.MAGENTA; // 紫色
        }
        
        String scoreText = (combo > 1) ? "+" + score + " x" + combo : "+" + score;
        system.emitTextParticle(x, y, 0, -60, 2.5f, textColor, scoreText);
        
        // 连击特效光环
        if (combo > 1) {
            createComboRingEffect(system, x, y, combo);
        }
        
        // 装饰粒子
        int particleCount = Math.min(6 + combo * 2, 20);
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float velX = (float)Math.cos(angle) * (50 + combo * 10);
            float velY = (float)Math.sin(angle) * (50 + combo * 10) - 30;
            
            int color = textColor;
            float size = 2 + combo * 0.5f + (float)Math.random() * 2;
            float life = 1.0f + combo * 0.2f + (float)Math.random() * 0.5f;
            
            system.emitAdvancedParticle(x + (-15 + (float)Math.random() * 30), 
                                      y + (-15 + (float)Math.random() * 30), 
                                      velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.96f);
        }
    }
    
    /**
     * 创建连击光环效果
     */
    private static void createComboRingEffect(ParticleSystem system, float x, float y, int combo) {
        int ringParticles = combo * 4;
        float radius = 30 + combo * 5;
        
        for (int i = 0; i < ringParticles; i++) {
            float angle = (float)((i / (float)ringParticles) * 2 * Math.PI);
            float startX = x + (float)Math.cos(angle) * radius;
            float startY = y + (float)Math.sin(angle) * radius;
            
            float velX = (float)Math.cos(angle) * 30;
            float velY = (float)Math.sin(angle) * 30;
            
            int color = Color.rgb(255, 215, 0); // 金色
            float size = 3;
            float life = 0.8f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.95f);
        }
    }
    
    /**
     * 创建护盾充能特效
     */
    public static void createShieldChargeEffect(ParticleSystem system, float x, float y, 
                                              float radius) {
        int particleCount = 15;
        
        for (int i = 0; i < particleCount; i++) {
            // 从外向内聚集
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float startX = x + (float)Math.cos(angle) * (radius + 30);
            float startY = y + (float)Math.sin(angle) * (radius + 30);
            
            float velX = -(float)Math.cos(angle) * 60; // 向中心聚集
            float velY = -(float)Math.sin(angle) * 60;
            
            int color = Color.rgb(100, 150, 255); // 蓝色能量
            float size = 3 + (float)Math.random() * 2;
            float life = 1.0f + (float)Math.random() * 0.5f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.98f);
        }
    }
    
    /**
     * 创建时间减缓特效
     */
    public static void createSlowTimeEffect(ParticleSystem system, float x, float y) {
        int particleCount = 20;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float radius = 40 + (float)Math.random() * 30;
            float startX = x + (float)Math.cos(angle) * radius;
            float startY = y + (float)Math.sin(angle) * radius;
            
            // 缓慢旋转运动
            float velX = -(float)Math.sin(angle) * 40; // 切线方向
            float velY = (float)Math.cos(angle) * 40;
            
            int color = Color.rgb(128, 0, 128); // 紫色时间粒子
            float size = 2 + (float)Math.random() * 3;
            float life = 2.0f + (float)Math.random() * 1.0f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.99f);
        }
    }
    
    /**
     * 创建双倍得分特效
     */
    public static void createDoubleScoreEffect(ParticleSystem system, float x, float y) {
        // "x2"文字特效
        system.emitTextParticle(x, y - 20, 0, -40, 2.0f, Color.GREEN, "x2");
        
        // 绿色光芒粒子
        int particleCount = 12;
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float velX = (float)Math.cos(angle) * 70;
            float velY = (float)Math.sin(angle) * 70 - 20;
            
            int color = Color.rgb(50, 205, 50); // 亮绿色
            float size = 3 + (float)Math.random() * 2;
            float life = 1.5f + (float)Math.random() * 0.5f;
            
            system.emitAdvancedParticle(x, y, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 20.0f, 0.97f);
        }
    }
    
    /**
     * 创建新纪录庆祝特效（主方法）
     */
    public static void createNewRecordCelebration(ParticleSystem system, float x, float y) {
        // 调用原有的新纪录特效
        createNewRecordEffect(system, x, y);
        
        // 添加额外的庆祝元素
        createVictoryCrown(system, x, y - 50);
        createGoldenShower(system, x, y);
    }
    
    /**
     * 创建等级提升特效
     */
    public static void createLevelUpEffect(ParticleSystem system, float x, float y) {
        // 发光上升文字效果
        createLevelUpText(system, x, y);
        
        // 螺旋上升的星星
        createSpiralStars(system, x, y);
        
        // 扩散光环
        createLevelUpRing(system, x, y);
    }
    
    /**
     * 创建胜利皇冠效果
     */
    private static void createVictoryCrown(ParticleSystem system, float x, float y) {
        int particleCount = 12;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float radius = 40 + (float)Math.random() * 20;
            float startX = x + (float)Math.cos(angle) * radius;
            float startY = y + (float)Math.sin(angle) * radius;
            
            // 向上飞舞的金色粒子
            float velX = (float)Math.cos(angle) * 20;
            float velY = -80 - (float)Math.random() * 40;
            
            int color = Color.rgb(255, 215, 0); // 金色
            float size = 4 + (float)Math.random() * 2;
            float life = 2.0f + (float)Math.random() * 1.0f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 80.0f, 0.96f);
        }
    }
    
    /**
     * 创建金色雨花效果
     */
    private static void createGoldenShower(ParticleSystem system, float x, float y) {
        int particleCount = 20;
        
        for (int i = 0; i < particleCount; i++) {
            float startX = x + (-60 + (float)Math.random() * 120);
            float startY = y - 100 - (float)Math.random() * 50;
            
            // 下落的金色颗粒
            float velX = (-20 + (float)Math.random() * 40);
            float velY = 30 + (float)Math.random() * 40;
            
            int color = Color.rgb(255, 215, 0); // 金色
            float size = 2 + (float)Math.random() * 3;
            float life = 3.0f + (float)Math.random() * 1.0f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 150.0f, 0.94f);
        }
    }
    
    /**
     * 创建等级提升文字效果
     */
    private static void createLevelUpText(ParticleSystem system, float x, float y) {
        // 发光的"LEVEL UP!"文字（使用文字粒子）
        system.emitTextParticle(x, y, 0, -50, 3.0f, Color.rgb(144, 238, 144), "LEVEL UP!");
        
        // 周围的发光粒子
        int particleCount = 15;
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float velX = (float)Math.cos(angle) * 60;
            float velY = (float)Math.sin(angle) * 60 - 30;
            
            int color = Color.rgb(144, 238, 144); // 浅绿色
            float size = 3 + (float)Math.random() * 2;
            float life = 1.5f + (float)Math.random() * 0.5f;
            
            system.emitAdvancedParticle(x, y, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 40.0f, 0.95f);
        }
    }
    
    /**
     * 创建螺旋星星效果
     */
    private static void createSpiralStars(ParticleSystem system, float x, float y) {
        int particleCount = 8;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float radius = 20 + i * 5;
            float startX = x + (float)Math.cos(angle) * radius;
            float startY = y + (float)Math.sin(angle) * radius;
            
            // 螺旋上升运动
            float velX = (float)Math.cos(angle + Math.PI/2) * 40; // 切线方向
            float velY = -60 - (float)Math.random() * 20; // 向上
            
            int color = Color.YELLOW;
            float size = 5;
            float life = 2.0f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 30.0f, 0.97f);
        }
    }
    
    /**
     * 创建等级提升光环
     */
    private static void createLevelUpRing(ParticleSystem system, float x, float y) {
        int particleCount = 24;
        float radius = 80;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float startX = x + (float)Math.cos(angle) * radius;
            float startY = y + (float)Math.sin(angle) * radius;
            
            // 向外扩散
            float velX = (float)Math.cos(angle) * 100;
            float velY = (float)Math.sin(angle) * 100;
            
            int color = Color.rgb(144, 238, 144); // 浅绿色
            float size = 3;
            float life = 1.0f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.92f);
        }
    }
    
    /**
     * 创建护盾特效（支持GameOverState中的护盾阻挡效果）
     */
    public static void createShieldEffect(ParticleSystem system, float x, float y, float radius) {
        // 护盾波纹效果
        int particleCount = 16;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float startX = x + (float)Math.cos(angle) * radius;
            float startY = y + (float)Math.sin(angle) * radius;
            
            // 向外扩散的波纹
            float velX = (float)Math.cos(angle) * 80;
            float velY = (float)Math.sin(angle) * 80;
            
            int color = Color.rgb(100, 150, 255); // 蓝色护盾
            float size = 4;
            float life = 0.8f;
            
            system.emitAdvancedParticle(startX, startY, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 0, 0.94f);
        }
        
        // 中心闪光
        system.emitAdvancedParticle(x, y, 0, 0, 0.5f, Color.WHITE, 15,
                                  Particle.TYPE_CIRCLE, 0, 0.85f);
    }
    
    /**
     * 创建道具特效（支持道具收集反馈）
     */
    public static void createPowerUpEffect(ParticleSystem system, float x, float y, int type) {
        int color;
        String text = "";
        
        // 根据道具类型选择颜色和文字
        switch (type) {
            case 0: // 金币
                color = Color.YELLOW;
                text = "+100";
                break;
            case 1: // 护盾
                color = Color.rgb(100, 150, 255);
                text = "SHIELD";
                break;
            case 2: // 双倍得分
                color = Color.GREEN;
                text = "x2 SCORE";
                break;
            case 3: // 时间减缓
                color = Color.MAGENTA;
                text = "SLOW TIME";
                break;
            default:
                color = Color.WHITE;
                text = "POWER UP";
        }
        
        // 文字特效
        system.emitTextParticle(x, y - 10, 0, -40, 1.5f, color, text);
        
        // 周围的粒子爆发
        int particleCount = 12;
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float velX = (float)Math.cos(angle) * 80;
            float velY = (float)Math.sin(angle) * 80;
            
            float size = 2 + (float)Math.random() * 2;
            float life = 1.0f + (float)Math.random() * 0.5f;
            
            system.emitAdvancedParticle(x, y, velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 50.0f, 0.96f);
        }
    }
    
    /**
     * 创建新纪录庆祝特效（原有方法）
     */
    public static void createNewRecordEffect(ParticleSystem system, float x, float y) {
        // 大量彩色礼花
        int particleCount = 30;
        
        for (int i = 0; i < particleCount; i++) {
            float angle = (float)((i / (float)particleCount) * 2 * Math.PI);
            float speed = 120 + (float)Math.random() * 80;
            float velX = (float)Math.cos(angle) * speed;
            float velY = (float)Math.sin(angle) * speed - 50; // 向上偏移
            
            // 彩虹色
            int color;
            switch (i % 6) {
                case 0: color = Color.RED; break;
                case 1: color = Color.rgb(255, 165, 0); break; // 橙色
                case 2: color = Color.YELLOW; break;
                case 3: color = Color.GREEN; break;
                case 4: color = Color.BLUE; break;
                case 5: color = Color.MAGENTA; break;
                default: color = Color.WHITE; break;
            }
            
            float size = 4 + (float)Math.random() * 3;
            float life = 2.0f + (float)Math.random() * 1.0f;
            
            system.emitAdvancedParticle(x + (-30 + (float)Math.random() * 60), 
                                      y + (-20 + (float)Math.random() * 40), 
                                      velX, velY, life, color, size,
                                      Particle.TYPE_CIRCLE, 100.0f, 0.95f);
        }
        
        // 金色闪光
        for (int i = 0; i < 10; i++) {
            float velX = -100 + (float)Math.random() * 200;
            float velY = -100 + (float)Math.random() * 50;
            
            system.emitAdvancedParticle(x, y, velX, velY, 1.5f, Color.rgb(255, 215, 0), 6,
                                      Particle.TYPE_CIRCLE, 50.0f, 0.94f);
        }
    }
}